import { _decorator, Component, Label, Sprite, Node, SpriteAtlas, SpriteFrame } from 'cc';
import { GameModel } from './GameModel';
import { MultiPlatform } from '../../Basic/Model/Platform/MultiPlatform';
import { VideoEvent } from '../../Data/Types';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType } from '../Config/GameEnum';
import { DataMgr } from '../../Basic/Manager/DataManager';
import { Tool } from '../../Util/Tool';
import { VisitorAni } from '../Ani/VisitorAni';
import { MathUtil } from '../../Util/MathUtil';
import { GEEvent, GravityModel } from '../../Basic/Model/SDK/GravityModel';
const { ccclass, property } = _decorator;

@ccclass('VisitorItem')
export class VisitorItem extends Component {
  @property(Sprite)
  itemSpr: Sprite;
  @property(Sprite)
  itemColor: Sprite;
  @property(Sprite)
  itemIcon: Sprite;
  @property(Label)
  itemCount: Label;
  @property(Node)
  messageNode: Node;
  @property(Node)
  btn_gold: Node;
  @property(Node)
  btn_ad: Node;
  @property(Label)
  lockLab: Label;
  @property(VisitorAni)
  animCtrl: VisitorAni;
  curColor: string;
  curCount: number;
  maxCount: number;
  curIndex: number;
  roleIndex: number;
  setData(color: string, count: number) {
    this.curColor = color;
    this.curCount = count;
    this.maxCount = count;
    this.roleIndex = this.curColor != '' ? MathUtil.randomIntegerBetween(1, 8) : 0;
  }

  setLockState(index: number) {
    this.curIndex = index;
    this.btn_gold.active = index == 1;
    this.btn_ad.active = index == 2;
  }

  protected onEnable(): void {
    this.refreshUI();
  }

  refreshUI() {
    const colorData = DataMgr.tables.TbColor.get(this.curColor);
    const imgData = colorData != null ? DataMgr.tables.TbImg.get(colorData.cakeImg) : null;
    const iconData = colorData != null ? DataMgr.tables.TbImg.get(colorData.fruitImg) : null;
    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
    if (imgData) {
      this.itemColor.spriteFrame = this.itemColor.spriteAtlas.getSpriteFrame(imgData.imgName);
    }
    // if (imgData) {
    //   if (imgData.altasPath === '') {
    //     GameModel.Ins()
    //       .loadDynamicRes<SpriteFrame>(imgData.imgName + '/spriteFrame')
    //       .then((spr) => {
    //         this.itemColor.spriteFrame = spr;
    //       });
    //   } else {
    //     GameModel.Ins()
    //       .loadDynamicRes<SpriteAtlas>(imgData.altasPath)
    //       .then((altas) => {
    //         this.itemColor.spriteFrame = altas.getSpriteFrame(imgData.imgName);
    //       });
    //   }
    // }
    // imgData &&
    //   Tool.getSpriteByKey(imgData.altasPath, imgData.imgName).then((sp) => {
    //     this.itemColor.spriteFrame = sp;
    //   });
    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
    if (iconData) {
      this.itemIcon.spriteFrame = this.itemColor.spriteAtlas.getSpriteFrame(iconData.imgName);
    }
    // if (iconData) {
    //   if (iconData.altasPath === '') {
    //     GameModel.Ins()
    //       .loadDynamicRes<SpriteFrame>(iconData.imgName + '/spriteFrame')
    //       .then((spr) => {
    //         this.itemIcon.spriteFrame = spr;
    //       });
    //   } else {
    //     GameModel.Ins()
    //       .loadDynamicRes<SpriteAtlas>(iconData.altasPath)
    //       .then((altas) => {
    //         this.itemIcon.spriteFrame = altas.getSpriteFrame(iconData.imgName);
    //       });
    //   }
    // }
    // iconData &&
    //   Tool.getSpriteByKey(iconData.altasPath, iconData.imgName).then((sp) => {
    //     this.itemIcon.spriteFrame = sp;
    //   });

    if (this.roleIndex === 0) {
      this.itemSpr.spriteFrame = null;
    } else {
      GameModel.Ins()
        .loadDynamicRes<SpriteFrame>(`public/sprite/visitor/visitor${this.roleIndex}/spriteFrame`)
        .then((spr) => {
          this.itemSpr.spriteFrame = spr;
        });
      // Tool.getSpriteByKey('').then((sp) => {
      //   this.itemSpr.spriteFrame = sp;
      // });
    }

    this.itemCount.string = `  ${this.curCount}`;
    this.messageNode.active = this.curColor != '' && !this.btn_gold.active && !this.btn_ad.active;
    // this.itemSpr.node.active = this.curColor != '' && !this.btn_gold.active && !this.btn_ad.active;
    this.itemIcon.node.active = this.curColor != '' && !this.btn_gold.active && !this.btn_ad.active;
    this.btn_gold.getComponentInChildren(Sprite).grayscale = this.curIndex === 1 && GameModel.Ins().gameCoin < 100;
    // if (this.btn_gold.active || this.btn_ad) return;
    // this.curColor != '' && this.animCtrl.curState != `Visitor_standby` && this.animCtrl.playAni('Visitor_in');
  }

  /** 减少数量 */
  reduceCount(color: string, count: number) {
    if (this.curCount <= 0) return count;
    if (this.curColor === color) {
      if (this.curCount >= count) {
        this.curCount -= count;
        for (let i = 0; i < count; i++) EventManager.Ins().emit(EventType.CREATE_FLY_COLOR, this.curColor, this.itemColor.node.getWorldPosition().clone());
        count = 0;
      } else {
        count -= this.curCount;
        for (let i = 0; i < this.curCount; i++) EventManager.Ins().emit(EventType.CREATE_FLY_COLOR, this.curColor, this.itemColor.node.getWorldPosition().clone());
        this.curCount = 0;
        // GameModel.Ins().finishOrder++;
      }
      // this.refreshUI();
    }
    return count;
  }

  reset() {
    this.curColor = ``;
    this.curCount = 0;
    this.maxCount = 0;
    // this.itemSpr.node.active = false;
    this.itemIcon.node.active = false;
    this.messageNode.active = false;
    this.itemCount.string = ``;
    this.itemColor.spriteFrame = null;
    this.itemIcon.spriteFrame = null;
    this.animCtrl.curState = 'Visitor_out';
    this.animCtrl.outting = false;
  }

  unlock() {
    this.btn_gold.active = false;
    this.btn_ad.active = false;
    this.reset();
    // this.refreshUI();
  }

  onClickBtn() {
    switch (this.curIndex) {
      case 1:
        if (GameModel.Ins().gameCoin >= 100) {
          GameModel.Ins().addGameMoney(-100);
          GameModel.Ins().orderMac.adVisitor = this.curIndex;
          GameModel.Ins().orderMac.onVisitorAd(false);
        }
        break;
      case 2:
        GameModel.Ins().orderMac.adVisitor = this.curIndex;
        MultiPlatform.Ins().platformUnit.showVideo(VideoEvent.UnlockVisitor);
        // GravityModel.I.track(GEEvent.AD_SHOW, { adPosition: `看广告解锁顾客栏` });
        break;
    }
  }
}
